package com.zmn.oms.zmn.business.impl.orderpay;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.math.MathUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.zmn.business.interfaces.orderpay.OrderPayVerifyBService;
import com.zmn.track.common.constant.TrackConfigConsts;
import com.zmn.track.common.constant.TrackConsts;
import com.zmn.track.common.dro.config.ConfTrackWorkDRO;
import com.zmn.track.dubbo.consts.DubboConsts;
import com.zmn.track.dubbo.dto.TrackWorkDIO;
import com.zmn.track.dubbo.dto.TrackWorkDRO;
import com.zmn.track.dubbo.dto.TrackWorkQuery;
import com.zmn.track.dubbo.interfaces.config.TrackWorkConfigListRemoteService;
import com.zmn.track.dubbo.interfaces.trackwork.TrackWorkListRemoteService;
import com.zmn.track.dubbo.interfaces.trackwork.TrackWorkModifyRemoteService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author sunlife
 * @date:2020/12/10 11:53 上午
 * description:
 */
@Service
public class OrderPayVerifyBServiceImpl implements OrderPayVerifyBService {

    @Autowired
    OrderDetailService orderDetailService;
    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    TrackWorkListRemoteService trackWorkListRemoteService;
    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    TrackWorkModifyRemoteService trackWorkModifyRemoteService;
    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    TrackWorkConfigListRemoteService trackWorkConfigListRemoteService;

    @Override
    public void verifyPayPosition(OrderWork orderWork, Double longitude, Double latitude) throws OmsBaseException {

        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderWork.getOrderId());

        double distance = MathUtil
                .GetDistance(orderDetail.getLongitude(), orderDetail.getLatitude(), longitude, latitude);

        if (distance > 2000) {
            TrackWorkQuery query = new TrackWorkQuery();
            query.setOrderId(orderWork.getOrderId());
            query.setWorkId(orderWork.getWorkId());
            query.setTrackContentId(TrackConfigConsts.TRACK_CONTENT_ID_NO_SCENE_PAY);
            ResponseDTO<List<TrackWorkDRO>> responseDTO = trackWorkListRemoteService.listTrackByQuery(query);
            List<TrackWorkDRO> trackWorkDROS = responseDTO.getData();
            if (CollectionUtil.isNullOrEmpty(trackWorkDROS)) {
                // 添加跟单
                TrackWorkDIO trackDIO = new TrackWorkDIO();
                trackDIO.setOrderId(orderWork.getOrderId());
                trackDIO.setWorkId(orderWork.getWorkId());
                trackDIO.setTrackContentId(TrackConfigConsts.TRACK_CONTENT_ID_NO_SCENE_PAY);
                trackDIO.setOperator("系统");
                trackDIO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
                trackDIO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);

                trackWorkModifyRemoteService.addTrack(trackDIO);

                // 抛出异常
                throw new OmsBaseException("未在现场支付，请联系客服人员解除限制");
            }
            TrackWorkDRO trackWorkDRO = trackWorkDROS.get(0);
            if (!Objects.equals(trackWorkDRO.getStatus(), TrackConsts.TRACK_STATUS_FINISHED)) {
                // 如果未完成，抛出异常
                throw new OmsBaseException("未在现场支付，请联系客服人员解除限制");
            }

        }

    }
}
